package com.castle.fortress.admin.member.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.member.dto.MemberPointsSerialDto;
import com.castle.fortress.admin.member.entity.MemberPointsSerialEntity;
import com.castle.fortress.admin.member.service.MemberPointsSerialService;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.utils.ConvertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会员积分流水 api 控制器
 *
 * @author Mgg
 * @since 2021-12-01
 */
@Api(tags="会员积分流水api管理控制器")
@RestController
@RequestMapping("/api/member/memberPointsSerial")
public class ApiMemberPointsSerialController {
    @Autowired
    private MemberPointsSerialService memberPointsSerialService;


    /**
     * 会员积分流水的分页展示
     * @param memberPointsSerialDto 会员积分流水实体类
     * @param currentPage 当前页
     * @param size  每页记录数
     * @return
     */
    @ApiOperation("会员积分流水分页展示")
    @GetMapping("/page")
    public RespBody<IPage<MemberPointsSerialDto>> pageMemberPointsSerial(MemberPointsSerialDto memberPointsSerialDto, @RequestParam(required = false) Integer currentPage, @RequestParam(required = false)Integer size){
        Integer pageIndex= currentPage==null? GlobalConstants.DEFAULT_PAGE_INDEX:currentPage;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<MemberPointsSerialDto> page = new Page(pageIndex, pageSize);

        IPage<MemberPointsSerialDto> pages = memberPointsSerialService.pageMemberPointsSerial(page, memberPointsSerialDto);
        return RespBody.data(pages);
    }

    /**
     * 会员积分流水保存
     * @param memberPointsSerialDto 会员积分流水实体类
     * @return
     */
    @ApiOperation("会员积分流水保存")
    @PostMapping("/save")
    public RespBody<String> saveMemberPointsSerial(@RequestBody MemberPointsSerialDto memberPointsSerialDto){
        if(memberPointsSerialDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            MemberPointsSerialEntity memberPointsSerialEntity = ConvertUtil.transformObj(memberPointsSerialDto,MemberPointsSerialEntity.class);
        if(memberPointsSerialService.save(memberPointsSerialEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 会员积分流水编辑
     * @param memberPointsSerialDto 会员积分流水实体类
     * @return
     */
    @ApiOperation("会员积分流水编辑")
    @PostMapping("/edit")
    public RespBody<String> updateMemberPointsSerial(@RequestBody MemberPointsSerialDto memberPointsSerialDto){
        if(memberPointsSerialDto == null || memberPointsSerialDto.getId() == null || memberPointsSerialDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            MemberPointsSerialEntity memberPointsSerialEntity = ConvertUtil.transformObj(memberPointsSerialDto,MemberPointsSerialEntity.class);
        if(memberPointsSerialService.updateById(memberPointsSerialEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 会员积分流水删除
     * @param ids 会员积分流水id集合
     * @return
     */
    @ApiOperation("会员积分流水删除")
    @PostMapping("/delete")
    public RespBody<String> deleteMemberPointsSerial(@RequestBody List<Long> ids){
        if(ids == null || ids.isEmpty()){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(memberPointsSerialService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 会员积分流水详情
     * @param id 会员积分流水id
     * @return
     */
    @ApiOperation("会员积分流水详情")
    @GetMapping("/info")
    public RespBody<MemberPointsSerialDto> infoMemberPointsSerial(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            MemberPointsSerialEntity memberPointsSerialEntity = memberPointsSerialService.getById(id);
            MemberPointsSerialDto memberPointsSerialDto = ConvertUtil.transformObj(memberPointsSerialEntity,MemberPointsSerialDto.class);
        return RespBody.data(memberPointsSerialDto);
    }


}
